/*
+--------------------------
| FreeloaderOS v0.0.1
|  - Microkernel
+--------------------------
| Written by Alec Hussey
| Copyright (c) 2007
|  - Deus X Foundation
+---------------------------
*/

/*
 * Interrupt Descriptor Table
 */

#include <sys/system.h>
#include <sys/idt.h>
#include <string.h>

void idt_set_gate(unsigned char num, 
                  unsigned long base, 
                  unsigned short sel, 
                  unsigned char flags)
{
	// Interrupt routine's base address
	idt[num].base_lo = (base & 0xFFFF);
	idt[num].base_hi = (base >> 16) & 0xFFFF;
	
	// Segment in which the IDT will use
	// is set, along with access flags
	idt[num].sel = sel;
	idt[num].always0 = 0;
	idt[num].flags = flags;
}

// Install the IDT
void init_idt()
{
	// Set special IDT pointer
	idtp.limit = (sizeof(struct idt_entry) * 256) - 1;
	idtp.base = (int)&idt;
	
	// Clear out entire IDT and init to zeros
	memset(&idt, 0, sizeof(struct idt_entry) * 256);
	
	// Add any new ISRs to the IDT here using idt_set_gate
	
	// Prints the CPUs internal register to the new IDT
	idt_load();
}
